package com.freemalll.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freemalll.common.core.utils.poi.ExcelUtil;
import com.freemalll.common.core.web.controller.BaseController;
import com.freemalll.common.core.web.domain.AjaxResult;
import com.freemalll.common.core.web.page.TableDataInfo;
import com.freemalll.common.log.annotation.Log;
import com.freemalll.common.log.enums.BusinessType;
import com.freemalll.common.security.annotation.RequiresPermissions;
import com.freemalll.common.security.utils.SecurityUtils;
import com.freemalll.system.domain.SysRoleResRel;
import com.freemalll.system.service.ISysRoleResRelService;

/**
 * 角色资源关系 信息操作处理
 * 
 * @author freemalll
 */
@RestController
@RequestMapping("/roleResRel")
public class SysRoleResRelController extends BaseController
{
    @Autowired
    private ISysRoleResRelService roleResRelService;

    /**
     * 获取角色资源关系列表
     */
    @RequiresPermissions("system:roleResRel:list")
    @GetMapping("/list")
    public TableDataInfo list(SysRoleResRel roleResRel)
    {
        startPage();
        List<SysRoleResRel> list = roleResRelService.selectRoleResRelList(roleResRel);
        return getDataTable(list);
    }

    @Log(title = "角色资源关系", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:roleResRel:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRoleResRel roleResRel)
    {
        List<SysRoleResRel> list = roleResRelService.selectRoleResRelList(roleResRel);
        ExcelUtil<SysRoleResRel> util = new ExcelUtil<SysRoleResRel>(SysRoleResRel.class);
        util.exportExcel(response, list, "角色资源关系数据");
    }

    /**
     * 根据角色资源关系编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(roleResRelService.selectRoleResRelById(id));
    }

    /**
     * 根据角色id查询关系列表
     */
    @GetMapping(value = "/roleId/{roleId}")
    public AjaxResult getInfoByRoleId(@PathVariable String roleId)
    {
        return success(roleResRelService.selectRoleResRelByRoleId(roleId));
    }

    /**
     * 根据资源id查询关系列表
     */
    @GetMapping(value = "/resourceId/{resourceId}")
    public AjaxResult getInfoByResourceId(@PathVariable String resourceId)
    {
        return success(roleResRelService.selectRoleResRelByResourceId(resourceId));
    }

    /**
     * 新增角色资源关系
     */
    @RequiresPermissions("system:roleResRel:add")
    @Log(title = "角色资源关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysRoleResRel roleResRel)
    {
        roleResRel.setCreateName(SecurityUtils.getUsername());
        return toAjax(roleResRelService.insertRoleResRel(roleResRel));
    }

    /**
     * 修改角色资源关系
     */
    @RequiresPermissions("system:roleResRel:edit")
    @Log(title = "角色资源关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysRoleResRel roleResRel)
    {
        roleResRel.setUpdateName(SecurityUtils.getUsername());
        return toAjax(roleResRelService.updateRoleResRel(roleResRel));
    }

    /**
     * 删除角色资源关系
     */
    @RequiresPermissions("system:roleResRel:remove")
    @Log(title = "角色资源关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        roleResRelService.deleteRoleResRelByIds(ids);
        return success();
    }
}